package com.bwie.boot.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.boot.dao.UserDao;
import com.bwie.boot.entity.User;
import com.bwie.boot.service.UserService;

import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import util.JwtUtil;

@RestController
@RequestMapping("user")
public class UserController {
     
	 @Autowired
	 private UserService service;
	    
	 @Autowired
	 private UserDao dao;
	 @Autowired
	  private JwtUtil jwtUtil;
	 @Autowired
		private HttpServletRequest request;
	//登录
	 /**
	  * 
	  * 
	  * 
	  * 
	  * @param
	  * 登录
	  */
	 @RequestMapping(value="/login",method=RequestMethod.POST)
	 public Result login(@RequestBody User user) {
		 System.out.println(user.getLoginname());
		 System.out.println(user.getPassword());
		User u=   service.findByLoginnameAndPassword(user.getLoginname(),user.getPassword());
		  if(u!=null) {
			  String token = jwtUtil.createJWT(u.getId(), u.getLoginname(), "jingli");
			  
		   Map map = new HashMap<>();
		   map.put("token", token);
		   map.put("name", u.getLoginname());
		   return  new Result(true, StatusCode.OK, "登录成功",map);
		  }else {
			  return  new Result(true, StatusCode.ERROR, "登录失败");
		  }
		 
		 
	 }
	 /*
	  * 
	  * 
	  */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public Result delete(@PathVariable String id) {
		/*System.out.println(id);
		  String header = request.getHeader("Authorization");
		  System.out.println(header);
		   if(header==null) {
			  return  new Result(false, StatusCode.ACCESSERROR, "权限不足");
		   }
		    if(!header.startsWith("Bearer ")) {
		    	 return new Result(false, StatusCode.ACCESSERROR, "权限不足");
		    }
		    //提取token
		    String token = header.substring(7);
		  
		     Claims jwt = jwtUtil.parseJWT(token);
		      
		        if(jwt==null) {
		        	 return new Result(false, StatusCode.ACCESSERROR, "权限不足"); 
		        	
		        	 
		        }
		       if(!"jingli".equals(jwt.get("roles"))) {
		    		return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		       }*/
		Claims claims=(Claims) request.getAttribute("jingli");
		System.out.println(claims);
		if(claims==null){
			return new Result(true,StatusCode.ACCESSERROR,"无权访问");
			}
		     service.delete(id);
		 	return new Result(true,StatusCode.OK,"删除成功");
		
	}
	
	 //短信
	 @RequestMapping(value="/sendsms/{mobile}",method=RequestMethod.POST)
	public Result sendsms(@PathVariable String mobile) {
		  service.sendSms(mobile);
		   return  new Result(true, StatusCode.OK, "发送成功");
	 }
	//用户注册
	@RequestMapping(value="/register/{code}",method=RequestMethod.POST)
	public Result  register(@RequestBody User user,@PathVariable String code) {
		System.out.println(code);
		System.out.println(user.getPassword());
		 service.add(user,code);
		   return new Result(true, StatusCode.OK, "注册成功");
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public Object list() {
		  List<User> findAll = dao.findAll();
		  return findAll;
	}
	//修改 粉丝数
	@RequestMapping(value="/incfans/{userid}/{x}",method=RequestMethod.POST)
    public void incFanscount(@PathVariable String userid,@PathVariable int x){
         service.incFanscount(userid,x);
    }
	//增加关注 数
	@RequestMapping(value="/incfollow/{userid}/{x}",method=RequestMethod.POST)
	public void incfollow(@PathVariable String userid,@PathVariable int x) {
		 service.incfollow(userid,x);
	}
}
